package com.umpay.privi.dao.on.sys.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.umpay.common.persist.AbstractDao;
import com.umpay.common.persist.ibatis.DataAccessException;
import com.umpay.common.persist.ibatis.exception.SQLErrorCodeSQLExceptionTranslator;
import com.umpay.privi.common.RetCode;
import com.umpay.privi.common.XmlData;
import com.umpay.privi.dao.EntityDaoImpl;
import com.umpay.privi.dao.on.sys.ResourceDao;
import com.umpay.privi.exception.BusinessException;
import com.umpay.privi.model.sys.Resource;

/** 
 * desc:资源Dao实现类
 * <p>创建人：Ding Zhe 创建日期：Apr 19, 2013 </p>
 * @version V1.0  
 */
public class ResourceDaoImpl extends EntityDaoImpl<Resource> implements ResourceDao {

	/* 
	 * desc:
	 * (non-Javadoc)
	 * @see com.umpay.pm.dao.on.sys.ResourceDao#get(java.lang.Integer)
	 */
	public Resource get(Integer resId )throws DataAccessException{
		try {
			return (Resource)getSqlMapClientTemplate().getSqlMapClient().queryForObject("Resource.Get", resId);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Resource> findAllSysResources(Map<String, Object> paraMap) throws DataAccessException {
		try {
			return getSqlMapClientTemplate().getSqlMapClient().queryForList("Resource.FindAllSysResources", paraMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public int statement(String sql) throws DataAccessException {
		try {
			return getSqlMapClientTemplate().getSqlMapClient().update("Resource.statement", sql);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public void updateResource(Map<String, Object> req)
			throws DataAccessException, BusinessException {
		try {
			//更新Flag标记为1
			int count = getSqlMapClientTemplate().getSqlMapClient().update("Resource.UpdateByMap", req);
			//判断更新记录数
			if (1 != count) {
				//更新记录数不为1，表示角色不存在
				throw new BusinessException(RetCode.NO_DATA,"资源信息不存在");
			}
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
		
	}

	public void deleteResource(Map<String, Object> req)
			throws DataAccessException, BusinessException {
		if (!req.containsKey(XmlData.RESOURCES_ID)) {
			throw new BusinessException(RetCode.REQ_PARA_MISSING,"资源号为必传字段");
		}
		logger.debug(String.format("[资源dao]标记删除资源，参数%s", req));
		try {
			//更新Flag标记为1
			int count = getSqlMapClientTemplate().getSqlMapClient().update("Resource.FlagDelete", req);
			//判断更新记录数
			if (1 != count) {
				//更新记录数不为1，表示角色不存在
				throw new BusinessException(RetCode.NO_DATA,"资源信息不存在");
			}
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
		
	}

	public Integer insterResource(Resource resource)
			throws DataAccessException, BusinessException {
		return (Integer) getSqlMapClientTemplate().insert("Resource.Insert",resource);
	}
}



